package com.shanzmoo.db.module.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shanzmoo.db.module.member.domain.query.MemberCouponConfigPageQuery;
import com.shanzmoo.db.module.member.domain.vo.MemberCouponConfigVO;
import com.shanzmoo.db.module.member.entity.MemberCouponConfigEntity;
import com.shanzmoo.db.module.member.mapper.MemberCouponConfigMapper;
import com.shanzmoo.db.module.member.service.IMemberCouponConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 优惠券配置 服务实现类
 * </p>
 *
 * @author wangyudi
 * @since 2021-04-06
 */
@Service
public class MemberCouponConfigServiceImpl extends ServiceImpl<MemberCouponConfigMapper, MemberCouponConfigEntity> implements IMemberCouponConfigService {

    @Override
    public IPage<MemberCouponConfigEntity> queryByPage(MemberCouponConfigPageQuery request) {
        Integer id = request.getId();//券编号
        String name = request.getName();//券名称
        Integer couponObj = request.getCouponObj();//发券对象
        Integer type = request.getType();//券类型
        Integer status = request.getStatus();//券状态

        String createTimeStart = request.getCreateTimeStart();
        String createTimeEnd = request.getCreateTimeEnd();

        String startTimeStart = request.getStartTimeStart();
        String startTimeEnd = request.getStartTimeEnd();


        String endTimeStart = request.getEndTimeStart();
        String endTimeEnd = request.getEndTimeEnd();

        String merchantCode = request.getMerchantCode();
        return page(
                request.page(),
                new LambdaQueryWrapper<MemberCouponConfigEntity>()
                .eq(id!=null,MemberCouponConfigEntity::getId,id)
                .eq(!StringUtils.isEmpty(name),MemberCouponConfigEntity::getName,name)
                .eq(couponObj!=null,MemberCouponConfigEntity::getCouponObj,couponObj)
                .eq(type!=null,MemberCouponConfigEntity::getType,type)
                .eq(status!=null,MemberCouponConfigEntity::getStatus,status)
                .between(!StringUtils.isEmpty(createTimeStart) && !StringUtils.isEmpty(createTimeEnd),MemberCouponConfigEntity::getCreateTime,createTimeStart,createTimeEnd)
                .between(!StringUtils.isEmpty(startTimeStart) && !StringUtils.isEmpty(startTimeEnd),MemberCouponConfigEntity::getStartTime,startTimeStart,startTimeEnd)
                .between(!StringUtils.isEmpty(endTimeStart) && !StringUtils.isEmpty(endTimeEnd),MemberCouponConfigEntity::getEndTime,endTimeStart,endTimeEnd)
                .eq(MemberCouponConfigEntity::getDeleted,false)
                .eq(!StringUtils.isEmpty(merchantCode),MemberCouponConfigEntity::getMerchantCode,merchantCode)

        );
    }

    @Override
    public void deleteById(Integer id) {
        MemberCouponConfigEntity memberCouponConfigEntity = new MemberCouponConfigEntity();
        memberCouponConfigEntity.setId(id);
        memberCouponConfigEntity.setDeleted(true);
        updateById(memberCouponConfigEntity);
    }

    @Override
    public Page<MemberCouponConfigVO> getPlfByRequest(MemberCouponConfigPageQuery request) {
        return baseMapper.queryPlfByRequest(request.page(),request);
    }
}
